﻿Ext.define("PSI.BaseCode.Runtime.MainForm", {

    config:{
        fid: "",
        name: "",
        reid: "",
        colModel:""
    },

    constructor: function (config) {
          this.initConfig(config);
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            // title: "码表结构管理",
            border: false,
            bodyBorder: false,
            layout: "border",
            items: [
                this.setMainPanel(),
                this.setBottorPanel()
            ]
        });

        me.panel = panel;
    },


    getMainPanel: function () {
        return this.panel;
    },

    setMainPanel: function () {

        var me = this;

        var pan = Ext.create("Ext.panel.Panel", {
            region: "north",
            height: "97%",
            width: 500,
            title: me.getName(),
            tbar: [
                { xtype: 'button', text: '新增' + me.getName(), iconCls: "psi-button-add", handler: this.addBCInfo, scope: this },
                { xtype: 'button', text: '编辑' + me.getName(), iconCls: "psi-button-edit", handler: this.editBCInfo, scope: this },
                { xtype: 'button', text: '删除' + me.getName(), iconCls: "psi-button-delete", handler: this.deleteBCInfo, scope: this }
            ],
            layout: "fit",
            border: 1,
            items: [
                me.getBCInfoGrid()
            ]
        })

        return pan;
    },

    setBottorPanel: function ()
    {
        var me=this
        
        var pan = Ext.create("Ext.panel.Panel", {
            id:"count",
            region: "south",
            height: "3%",
            layout: "fit",
            border: 1,
            html:"<lable style='font-size:14px;'>共有<span style='color:red'>0</span>条记录</lable>"
        });

        return pan;
    },

    getBCInfoGrid: function () {

        var me = this;

        var bcmodel= Ext.define("bcModel", {
            extend: "Ext.data.Model"
        });

        var bcStore = Ext.create("Ext.data.Store", {
            model: bcmodel,
            autoLoad: false,
            data:[]
        })
    
        Ext.Ajax.request({
            url: "/BaseCodeRuntime/GetColumnsItem",
            method: "POST",
            params: { basecodeid: me.getReid() },
            async: false,
            callback: function (options, success, response) {

                 if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    
                    me.setColModel(data);

                    var fields = [];

                    var col = [];

                    for (var i = 0; i < data.length; i++) {
                        fields.push(data[i].columnName);
                        if (data[i].isVisible == 1)
                        {
                            if (data[i].columnType == "datetime") {
                                col.push({
                                    header: data[i].columnTitle, dataIndex: data[i].columnName, menuDisabled: true, draggable: false, sortable: false, renderer: function (value) {
                                        var dt = value.replace("T", " ");

                                        var ff = Date.parse(dt.replace(/-/g, "/"));

                                        var newValue = new Date(ff);

                                        var time = Ext.Date.format(newValue, 'Y-m-d H:m');

                                        var tempstr = time.substr(0, 4);

                                        if (tempstr == "1901" || tempstr=="1900") {
                                            return "";
                                        }
                                        else {

                                            return time;
                                        }
                                    }
                                })
                            }
                            else {
                                col.push({ header: data[i].columnTitle, dataIndex: data[i].columnName, menuDisabled: true, draggable: false, sortable: false });
                            }
                        }
                    }
                    bcmodel.setFields(fields);
                    
                    var grid = Ext.create("Ext.grid.Panel", {
                        id:"bcgrid",
                      //  title: "本月进口计划",
                        columnLines: true,
                        columns: col,
                        border:false,
                        store: bcStore
                    });

                    me.gridinfo = grid;      
                }

            }

        });
        bcStore.setProxy({
            type: 'ajax',
            url: '/BaseCodeRuntime/GetInfoItem',
            extraParams: { basecodeid: me.getReid() },
            actionMethods: { read: 'POST' },
            reader: {
                type: 'json',
                root: 'Data'
            }
        });

        bcStore.load({
            callback: function (m,f,op) {
                Ext.getCmp("count").body.update("<lable style='font-size:14px;'>共有<span style='color:red'>"+m.length+"</span>条记录</lable>"); 
            }
        });

        return me.gridinfo
    },

    deleteBCInfo: function () {
        var me = this;
        var grid = this.gridinfo;
        var items = grid.getSelectionModel().getSelection();

        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要删除的信息");
            return;
        }

        var role = items[0].data;

        PSI.MsgBox.confirm("请确认是否删除该信息",
            function () {
                Ext.getBody().mask("正在删除中...");
                Ext.Ajax.request({
                    url: "/BaseCodeRuntime/DeleteInfo",
                    method: "POST",
                    params: { basecodeid: me.getReid(), infoid: role.bc_id },
                    callback: function (options, success, response) {
                        Ext.getBody().unmask();

                        if (success) {
                            var data = Ext.JSON.decode(response.responseText);
                            if (data.Success) {
                                PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                    var store = grid.getStore();
                                    store.load();
                                });
                            } else {
                                PSI.MsgBox.showInfo(data.Msg);
                            }
                        } else {
                            PSI.MsgBox.showInfo("网络错误", function () {
                                window.location.reload();
                            });
                        }
                    }
                });
            });

    },

    addBCInfo: function () {

        var me=this;
       
        var config={
            fid: me.getFid(),
            name:  me.getName(),
            reid:  me.getReid(),
            colModel: me.getColModel()
        }

        var form = Ext.create("PSI.BaseCode.Runtime.BaseCodeInfoEdit", config);

        form.show();

    },

    editBCInfo: function () {

        var me = this;
        var grid = this.gridinfo;
        var items = grid.getSelectionModel().getSelection();

        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的信息");
            return;
        }

        var role = items[0].data;


        var config = {
            fid: me.getFid(),
            name: me.getName(),
            reid: me.getReid(),
            colModel: me.getColModel()
        }

        var form = Ext.create("PSI.BaseCode.Runtime.BaseCodeInfoEdit", config);

        form.show(role);
    }

});